package com.gxa.controller.lgr.login;


import com.gxa.common.R;
import com.gxa.common.ResultDTO;
import com.gxa.common.vo.LoginUser;
import com.gxa.service.LoginService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @author MacieSerenity
 */
@Controller
@Api(value = "用户登录类", tags = "用户登录数据接口")
public class LoginController {
    @Autowired
    LoginService loginService;

    /**
     * 使用用户管理下的用户名和密码进行登录，<br/>
     * 密码默认是123456
     *
     * @param loginUser 登录用户的实体类，由用户名，密码和验证码组成
     * @return 200代表添加成功；<br/>
     * 400代表参数格式不对或未携带验证码<br/>
     * 404代表找不到此请求;<br/>
     * 500代表后端代码出现异常;<br/>
     * 11011 登陆失败，用户名或者密码错误；<br/>
     * 11012 验证码不正确；
     */
    @PostMapping("/login")
    @ResponseBody
    @ApiOperation(value = "用户登录接口", notes = "根据用户名和密码进行登录，需要额外传入一个验证码参数", httpMethod = "POST", response = R.class)
    @ApiResponses({
            @ApiResponse(code = 400, message = "参数格式不对或未携带验证码"),
            @ApiResponse(code = 404, message = "找不到此请求"),
            @ApiResponse(code = 500, message = "后端代码出现异常"),
            @ApiResponse(code = 11011, message = "登陆失败，用户名或者密码错误"),
            @ApiResponse(code = 11012, message = "验证码不正确"),
    })
    public ResultDTO login(@RequestBody @ApiParam(name = "username", value = "用户")
                   LoginUser loginUser, HttpServletRequest request, HttpServletResponse response) {
        System.out.println(loginUser);
        return loginService.userLogin(loginUser, request, response);
    }

    @GetMapping("/logOut")
    @ResponseBody
    @ApiOperation(value = "用户登出", notes = "登出并清除所有信息", httpMethod = "GET", response = R.class)
    @ApiResponses({
            @ApiResponse(code = 400, message = "参数格式不对或未携带验证码"),
            @ApiResponse(code = 404, message = "找不到此请求"),
            @ApiResponse(code = 500, message = "后端代码出现异常"),
            @ApiResponse(code = 11011, message = "登陆失败，用户名或者密码错误"),
            @ApiResponse(code = 11012, message = "验证码不正确"),
    })
    public R logOut() {
        return loginService.logOut();
    }


    @GetMapping("/login/captcha.jpg")
    @ResponseBody
    @ApiOperation(value = "用户登录时所需要的验证码图片", notes = "访问接口返回JSON和图片", httpMethod = "GET", response = R.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    public R getEasyCaptcha(HttpServletRequest request, HttpServletResponse response) {
        return loginService.returnCaptcha(request, response);
    }
}
